package pos.dato;

import java.sql.*;

import pos.dominios.Perfil;

public class JDBCPerfilDAO implements IPerfilDAO {

public void delete(Connection conn, String perfilID) {
        String sql = "DELETE FROM Perfiless WHERE (perfilID = ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, perfilID);
            stmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("Message: " + e.getMessage());
            System.err.println("SQLState: " + e.getSQLState());
            System.err.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }
        }
    }

    public void insert(Connection conn, String perfilID, Perfil p) {
        String sql = "INSERT INTO Perfiles ( perfilID, nombre, apellidos, dni, email, telefono, direccion, ciudad) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, perfilID);
            stmt.setString(2, p.getNombre());
            stmt.setString(3, p.getApellidos());
            stmt.setString(4, p.getDni());
            stmt.setString(5, p.getEmail());
            stmt.setString(6, p.getTelefono());
            stmt.setString(7, p.getDireccion());
            stmt.setString(8, p.getCiudad());

            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

    }

    public Perfil select(Connection conn, String perfilID) {
        String sql = "SELECT * FROM Perfiles WHERE (perfilID = ? ) ";
        PreparedStatement stmt = null;
        ResultSet result = null;
        Perfil p = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, perfilID);

            result = stmt.executeQuery();

            result.next();
            p = new Perfil();
            p.setNombre(result.getString("nombre"));
            p.setApellidos(result.getString("apellidos"));
            p.setDni(result.getString("dni"));
            p.setEmail(result.getString("email"));
            p.setTelefono(result.getString("telefono"));
            p.setDireccion(result.getString("direccion"));
            p.setCiudad(result.getString("ciudad"));
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }

        return p;
    }

}
